package com.lucien.system.service.impl;

import com.lucien.system.domain.ChatMessage;
import com.lucien.system.domain.SysGroupMessage;
import com.lucien.system.domain.SysGroupMessageExample;
import com.lucien.system.mapper.SysGroupMapper;
import com.lucien.system.mapper.SysGroupMessageMapper;
import com.lucien.system.service.SysGroupMemberService;
import com.lucien.system.service.SysGroupMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysGroupMessageServiceImpl implements SysGroupMessageService {

    @Autowired
    private SysGroupMessageMapper groupMessageMapper;

    @Autowired
    private SysGroupMemberService groupMemberService;

    @Autowired
    private SysGroupMapper groupMapper;

    /**
     * 新增群组聊天记录
     * @param groupMessage
     * @return
     */
    @Override
    public int insert(SysGroupMessage groupMessage) {
        if (groupMessageMapper.insert(groupMessage) != 1){
            return -1;
        }
        return 1;
    }

    /**
     * 获取群组聊天记录
     * @param groupId
     * @return
     */
    @Override
    public List<SysGroupMessage> listGroupMsg(Integer groupId) {
        SysGroupMessageExample example = new SysGroupMessageExample();
        example.setOrderByClause("create_time asc");
        SysGroupMessageExample.Criteria criteria = example.createCriteria();
        criteria.andGroupIdEqualTo(groupId);

        List<SysGroupMessage> groupMessages = groupMessageMapper.selectByExample(example);

        return groupMessages;
    }

    /**
     * 删除群组聊天记录
     * @param groupId
     * @return
     */
    @Override
    public int delGroupMsg(Long groupId) {
        SysGroupMessageExample example = new SysGroupMessageExample();
        SysGroupMessageExample.Criteria criteria = example.createCriteria();
        criteria.andGroupIdEqualTo(Math.toIntExact(groupId));

        return groupMessageMapper.deleteByExample(example);
    }

    /**
     * 获取用户所在群聊的最近一条消息
     * @param userId
     * @return
     */
    @Override
    public List<SysGroupMessage> userGroupMsgList(Integer userId) {
        List<SysGroupMessage> result = new ArrayList<>();
        // 用户所在群聊的ID
        Integer[] groupIds = groupMemberService.selectByUserId(userId);

        for (Integer groupId : groupIds) {
            SysGroupMessageExample example = new SysGroupMessageExample();
            example.setOrderByClause("create_time desc");
            SysGroupMessageExample.Criteria criteria = example.createCriteria();
            criteria.andGroupIdEqualTo(groupId);

            List<SysGroupMessage> groupMessages = groupMessageMapper.selectByExample(example);

            if (groupMessages.size() != 0){
                result.add(groupMessages.get(0));
            }
        }

        return result;
    }

    @Override
    public List<ChatMessage> frontChatMsg(Long groupId) {
        List<ChatMessage> result = new ArrayList<>();
        List<SysGroupMessage> groupMessages = listGroupMsg(Math.toIntExact(groupId));
        for (SysGroupMessage message : groupMessages) {
            ChatMessage chatMessage = toChatMsg(message, groupMapper.selectByPrimaryKey(groupId).getName());
            result.add(chatMessage);
        }

        return result;
    }


    /**
     * groupmsg转为chatmsg
     * @param groupMessage
     * @param groupName
     * @return
     */
    private static ChatMessage toChatMsg(SysGroupMessage groupMessage, String groupName){
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setId(Math.toIntExact(groupMessage.getId()));
        chatMessage.setFromId(groupMessage.getUserId());
        chatMessage.setFromName(groupMessage.getUsername());
        chatMessage.setFromProfile(groupMessage.getUserProfile());
        chatMessage.setToId(groupMessage.getGroupId());
        chatMessage.setToName(groupName);
        chatMessage.setMessageTypeId(groupMessage.getMessageTypeId());
        chatMessage.setCreateTime(groupMessage.getCreateTime());
        chatMessage.setContent(groupMessage.getContent());

        return chatMessage;
    }
}
